package com.tandashi.springbootstudy01.study.reflection;

import java.lang.annotation.*;

public class Main {

    public static void main(String[] args) {
        Class<Sub> clazz = Sub.class;

        System.out.println("isAnnotationPresent SupperAnnotation = "+clazz.isAnnotationPresent(SupperAnnotation.class));
        System.out.println("isAnnotationPresent SubAnnotation = "+clazz.isAnnotationPresent(SubAnnotation.class));

        Annotation[] annotations = clazz.getAnnotations();
        System.out.println("getAnnotations =========");
        for (Annotation anno: annotations) {
            System.out.println(anno.toString());
        }

        System.out.println("getAnnotation SupperAnnotation ====");
        SupperAnnotation SupAnnotation = clazz.getAnnotation(SupperAnnotation.class);
        System.out.println(SupAnnotation);

        System.out.println("getAnnotation SubAnnotation ====");
        SubAnnotation SubAnnotation = clazz.getAnnotation(SubAnnotation.class);
        System.out.println(SubAnnotation);

        System.out.println("getDeclaredAnnotation SubAnnotation ====");
        SubAnnotation subAnnotation = clazz.getDeclaredAnnotation(SubAnnotation.class);
        System.out.println(subAnnotation.toString());

        System.out.println("getDeclaredAnnotation SupperAnnotation ====");
        SupperAnnotation supperAnnotation = clazz.getDeclaredAnnotation(SupperAnnotation.class);
        System.out.println(supperAnnotation);

        System.out.println("getDeclaredAnnotationsByType SubAnnotation ====");
        SubAnnotation[] subAnnotations = clazz.getDeclaredAnnotationsByType(SubAnnotation.class);
        for (Annotation sub: subAnnotations) {
            System.out.println(sub.toString());
        }

        System.out.println("getDeclaredAnnotationsByType SupperAnnotation ====");
        SupperAnnotation[] supperAnnotations = clazz.getDeclaredAnnotationsByType(SupperAnnotation.class);
        for (Annotation sup: supperAnnotations) {
            System.out.println(sup);
        }

        System.out.println("getAnnotationsByType SubAnnotation ====");
        SubAnnotation[] subAnnos = clazz.getAnnotationsByType(SubAnnotation.class);
        for (Annotation sub: subAnnos) {
            System.out.println(sub.toString());
        }

        System.out.println("getAnnotationsByType SupperAnnotation ====");
        SupperAnnotation[] supperAnnos = clazz.getAnnotationsByType(SupperAnnotation.class);
        for (Annotation sup: supperAnnos) {
            System.out.println(sup);
        }


    }


    @SupperAnnotation
    private static class Sup {

    }

    @SubAnnotation
    private static class Sub extends Sup {

    }

    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    @Target(ElementType.TYPE)
    private @interface SupperAnnotation {

        String value() default "SupperAnnotation";
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    @Target(ElementType.TYPE)
    private @interface SubAnnotation {

        String value() default "SubAnnotation";
    }
}
